import React, {Component} from 'react';
import {ajax, cacheTools} from 'nc-lightapp-front';
import  Pagination from 'src/hrpub/common/components/Pagination/index';
import { getAppPageConfig } from 'src/hrpub/common/utils/utils';
const appcode = getAppPageConfig().appcode, pagecode = getAppPageConfig().pagecode;
import './index.less'
class TableList extends Component{
    constructor(props) {
        super(props);
        this.state = {
            pageInfo: {
                pageIndex: 1,
                pageSize: 10
            },
            pk_org: '',
            conditions: null,
            // pk_psndoc_subs: null,
            pk_ctrtmngs: null,
            showPagnation: false
        }
    }
    // 查询表格数据并显示
    queryTableList (pk_org, pk_ctrtmngs, condition, pkIndex) {
        // let queryType = 0
        // if (pk_psndoc_subs) {
        //     queryType = 1
        // }
        let {pageInfo, conditions} = this.state;
        let props = this.props;
        let {editTable, otherParams} = props;
        let defaultConditions = {
            "querycondition":{
                "logic":"and",
                "conditions": [{
                    "field":"sign_state",
                    "value":{
                        "firstvalue": "-1",
                        "secondvalue": ""
                    }
                },{
                    "field":"sign_state",
                    "value":{
                        "firstvalue": "3",
                        "secondvalue": ""
                    }
                }]
            },
            "nctz":8,
            "beginTime":"",
            "billStatus":['-1', '3'],
            "endTime":"",
            "time":"oneWeek"
        }
        if(condition && !condition['billStatus']) {
            let time = otherParams
            if(!Object.keys(time).length) {
                time = {
                    beginTime: "",
                    billStatus: ['-1', '3'],
                    endTime: "",
                    time: "oneWeek"
                }
            }
            // time里包含除组织外的全部选择数据
            Object.assign(condition, time);
        }
        let fliterConditions = condition ? condition : conditions ? conditions : defaultConditions; //先判断参数是否存在，如果不存在调用state中是否存在，如果不存在调用默认值
        // debugger
        let paramObj = {
            pk_org: pk_org,
            areaCode: 'dataList',
            appCode: appcode,
            pageCode: pagecode,
            pageInfo: pageInfo,
            // queryType,
            // pkPsndocSub: pk_psndoc_subs,
            ...fliterConditions
        }
        ajax({
            url: '/nccloud/hrcm/ctrtmng/CtrtMngQueryListAction.do',
            data: paramObj,
            success: (result) => {
                if (result.success) {
                    props.setBtnVisibleAndDisable('onlyTableListStatus');
                    if (props.returnListOrCard() === 'tableList') {
                        props.setBtnVisibleAndDisable('addDeleteMakeEffectRefreshQuery')
                        if (!result.data) {
                            props.setBtnVisibleAndDisable('onlyAddAndRefreshNotDisabled')
                            editTable.setTableData('dataList', {rows: []})
                            this.setState({
                                showPagnation: false
                            })
                            return false
                        } else {
                            props.setBtnVisibleAndDisable('allBtnEnabled')
                        }
                    } else {
                        props.setBtnVisibleAndDisable('notShowSaveAndCancel')
                        if (!result.data) {
                            props.setBtnVisibleAndDisable('onlyAddBtnEnabled')
                            editTable.setTableData('dataList', {rows: []})
                            return false
                        } else {
                            props.setBtnVisibleAndDisable('allBtnEnabled')
                        }
                    }
                    let dataList = result.data.dataList
                    editTable.setTableData('dataList', dataList)
                    let rows = dataList.rows
                    let allpks = rows.map(function (item) {
                        return item.values.pk_ctrtmng.value
                    })
                    cacheTools.set('allpks', allpks)
                    if (props.returnListOrCard() === 'cardList' && pkIndex) {
                        // debugger
                        let allpks = cacheTools.get('allpks')
                        let id = null
                        if (pkIndex === 'length - 1') {
                            id = allpks[allpks.length - 1]
                        } else {
                            id = allpks[pkIndex]
                        }
                        props.setRecord({values: {pk_ctrtmng: {value: id}}})
                        props.handlePageInfoChange(this.props, id, 1)
                    }
                    this.setState({
                        pageInfo: dataList.pageInfo,
                        showPagnation: true,
                        pk_org,
                        conditions: fliterConditions,
                        pk_ctrtmngs
                    })
                }else{
                    props.setRecord({values: {pk_ctrtmng: {value: ''}}})
                }
            }
        })
    }
    // 选择框有变动的钩子函数
    selectedChange (props, moduleId, newVal, oldVal) {
        
    }
    //分页事件
    paginationEve (key) {
        const {pk_org, conditions, pk_ctrtmngs} = this.state
        let pageInfo = this.state.pageInfo
        pageInfo.pageIndex = key
        this.setState({
            pageInfo
        }, function () {
            this.queryTableList(pk_org, pk_ctrtmngs, conditions)
        })
    }
    // 每页显示条数
    pageSizeSelect (val) {
        const {pk_org, conditions, pk_ctrtmngs} = this.state
        let pageInfo = this.state.pageInfo
        pageInfo.pageSize = val
        this.setState({
            pageInfo
        }, function () {
            this.queryTableList(pk_org, pk_ctrtmngs, conditions)
        })
    }
    getHeight = () => {
		let wH = window.innerHeight,
			gap = 170;
		return wH - gap;
    };
    // 双击行事件
    onRowDoubleClick (record, index, props, event) {
        this.props.toCardList(record)
    }
    render () {
        const {editTable} = this.props
        let state = this.state
        let H = this.getHeight()
        return (
            <div className="tableList nc-bill-table-area">
                {
                    editTable.createEditTable('dataList', {
                        // showIndex: true,
                        showCheck: true,
                        cancelCustomRightMenu: true,
                        height: H,
                        onRowDoubleClick: this.onRowDoubleClick.bind(this),
                        selectedChange: this.selectedChange.bind(this)
                    })
                }
                <div className="pagination">
                    {state.showPagnation ? <Pagination
                        total = {state.pageInfo.total}
                        pageSize = {state.pageInfo.pageSize}
                        showQuickJumper ={true}
                        showSizeChanger = {true}
                        // current = {state.pageInfo.pageIndex}
                        onChange = {this.paginationEve.bind(this)}
                        onShowSizeChange = {this.pageSizeSelect.bind(this)}
                    /> :null}
                </div>
            </div>
        )
    }
}

export default TableList